<template>
  <van-list
    v-model="loading"
    :finished="finished"
    finished-text="没有更多了"
    :immediate-check="true"
    offset="50"
    @load="onLoad"
  >
    <slot :list="list"></slot>
  </van-list>
</template>
<script>
import Vue from 'vue'
import { List } from 'vant'
import { getGoodListApi } from '@/api/haigou'

Vue.use(List)

export default {
  name: 'CommonGoodContainer',
  props: ['params', 'method'],
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      count: 1
    }
  },
  watch: {
    params: {
      handler () {
        this.resetPullUpLoad()
      }
    }
  },
  methods: {
    async getGoodList () {
      try {
        const list = await getGoodListApi({ count: this.count, limitNum: 10, ...this.params })
        this.count++
        this.loading = false
        if (!list.length) this.finished = true
        this.list = this.list.concat(list)
      } catch (e) {}
    },
    onLoad () {
      console.log('load')
      this.getGoodList()
    },
    resetPullUpLoad () {
      this.list = []
      this.count = 1
      this.finished = false
    },
    async pullRefresh () {
      this.resetPullUpLoad()
      await this.getGoodList()
    }
  }
}
</script>
